/**
 * 
 */
package com.ra.landlord;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collection;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.googlecode.cswish.annotation.Rest;
import com.googlecode.cswish.model.PageInfo;
import com.googlecode.cswish.struts.spring.GenericService;
import com.googlecode.cswish.util.bean.ModelUtil;

/**
 * 分摊单元服务
 * @author colin
 * @date 2015年3月14日
 */
@Service
public class ShareUnitService {
	
	@Resource
	private GenericService genericService;
	
	@Resource
	private LandlordUserService landlordUserService;
	
	/**
	 * 查询分摊单元列表
	 * @param model
	 * @return
	 */
	@Rest(type = "search")
	public List<ShareUnit> searchShareUnit(ShareUnit model){
		PageInfo<ShareUnit> pageInfo = genericService.searchAll(model);
		return pageInfo.getData();
	}
	
	/**
	 * 新增分摊单元
	 * @param model
	 * @return
	 */
	@Rest(type = "add")
	public ShareUnit addShareUnit(ShareUnit model){
		
		List<FeeRule> lstFeeRule = model.getFeeRules();
		if (lstFeeRule != null) {
			for (FeeRule feeRule : lstFeeRule) {
				if (feeRule.getFeeRuleCfg() != null) {
					FeeRuleCfg feeRuleCfg = genericService.load(feeRule.getFeeRuleCfg());
					feeRule.setFeeRuleCfg(feeRuleCfg);
					feeRule.setShareUnit(model);
				}
			}
		}
		
		return genericService.add(model);
	}
	
	/**
	 * 修改分摊单元
	 * @param model
	 * @return
	 */
	@Rest(type = "update")
	public ShareUnit updateShareUnit(ShareUnit model){
		Collection<String> enableFields = Arrays.asList("name", "description", "rooms", "feeRules");
		return genericService.update(model, false, enableFields, null);
	}
	
	/**
	 * 删除分摊单元
	 * @param model
	 * @return
	 */
	@Rest(type = "delete")
	public boolean deleteShareUnit(List<Object> models){
		return genericService.deleteList(models);
	}
	
	/**
	 * 根据仪表查询房屋
	 * @param buildingId
	 * @param meterTypeId
	 * @return
	 */
	@Rest(type = "search")
	public List<ShareUnit> searchWithMeter(Integer buildingId, Integer feeRuleCfgDescID) {
		
		// 未抄表
		ShareUnit model = new ShareUnit();
		ModelUtil.setProperty(model, "building.id", buildingId);
		
		PageInfo<ShareUnit> pageInfo = genericService.searchAll(model);
		List<ShareUnit> shareUnits = new ArrayList<>();
		for (ShareUnit shareUnit : pageInfo.getData()) {
			FeeRule matchedFeeRule = null;
			for (FeeRule feeRule : shareUnit.getFeeRules()) {
				if (feeRule != null) {
					if (feeRuleCfgDescID.equals(feeRule.getFeeRuleCfg().getFeeRuleCfgDesc().getId())) {
						matchedFeeRule = feeRule;
						break;
					}
				}
			}
			if (matchedFeeRule != null) {
				List<Meter> lstMeter = matchedFeeRule.getMeters();
				
				if (lstMeter == null || lstMeter.size() == 0) {
					//初始化表刻度
					Meter initMeter = new Meter();
					initMeter.setCount(BigDecimal.valueOf(0));
					initMeter.setFeeRule(matchedFeeRule);
					initMeter.setCheckTime(null);
					shareUnit.setLastMeter(initMeter);
					shareUnits.add(shareUnit);
				}else if(lstMeter.size() >= 1){
					shareUnit.setLastMeter(lstMeter.get(0));
					if (lstMeter.size() > 1) {
						shareUnit.setLastLastMeter(lstMeter.get(1));
					}
					shareUnits.add(shareUnit);
				}
			}
		}
		return shareUnits;
	}
	
}
